package com.tanhua.server.controller;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.server.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    //判断用户是否为冻结
    //参数 id 和 冻结范围
    public void checkUserStatus(String status,Long id){
      //拼接key
        String key =Constants.USER_FREEZE+id;
        String value     = redisTemplate.opsForValue().get(key);
        //如果数据存在，且冻结范围一样的，抛出异常
        if(!StringUtils.isEmpty(value)){//如果不是空
            Map map = JSON.parseObject(value, Map.class);
            String freezingRange = map.get("freezingRange").toString();
            if(status.equals(freezingRange)){
                    throw new BusinessException(ErrorResult.builder().errMessage("用户已冻结").build()   );
            }
        }

    }
}
